#include "updatepassword.h"
#include "ui_updatepassword.h"
#include <QMouseEvent>
#include <QMessageBox>
#include "QNetworkRequest"
#include "QNetworkReply"


updatePassWord::updatePassWord(QWidget *parent, Person * user) :
    QWidget(parent),
    ui(new Ui::updatePassWord),
    passWord_user(user)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

updatePassWord::~updatePassWord()
{
    delete ui;
}

void updatePassWord::basicSettings()
{
    //消除边框
    this->setWindowFlag(Qt::FramelessWindowHint);

    ui->labPixmap->setScaledContents(true);
    ui->labPixmap->setPixmap(QPixmap(":/picture/password.jpg"));

    //账号文本框不可编辑
    ui->editId->setEnabled(false);
    ui->editId->setText(passWord_user->getId());
    //密码框
    ui->editPassWord->setEchoMode(QLineEdit::Password);
    ui->editNewPW->setEchoMode(QLineEdit::Password);
    ui->editNewAgain->setEchoMode(QLineEdit::Password);

    ui->editPassWord->setMaxLength(8);
    ui->editNewPW->setMaxLength(8);
    ui->editNewAgain->setMaxLength(8);
}

void updatePassWord::connectSettings()
{
    //关闭与最小化
    connect(ui->btnClose, &QPushButton::clicked, this, &updatePassWord::closePassWord);
    connect(ui->btnMinimize, &QPushButton::clicked, this, &QWidget::showMinimized);

    //修改与取消
    connect(ui->btnCancel, &QPushButton::clicked, this, &updatePassWord::closePassWord);
    connect(ui->btnUpdate, &QPushButton::clicked, this, &updatePassWord::updateJudge);
}

void updatePassWord::updateJudge()
{
    if(QString::compare(passWord_user->getPassWord(), ui->editPassWord->text()) == 0)
    {
        if(QString::compare(ui->editNewPW->text(), ui->editNewAgain->text()) == 0)
        {
            if(QString::compare(passWord_user->getPassWord(), ui->editNewPW->text()) != 0)
            {
                //修改密码请求
                // URL
                QString baseUrl = "http://127.0.0.1:5000/PasswordUpdate?Userid=" + passWord_user->getId() + "&Password=" + ui->editNewPW->text();
                QUrl url(baseUrl);

                // 构造请求QNetworkRequest
                QNetworkRequest request;
                request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
                request.setUrl(url);

                QNetworkAccessManager *manager = new QNetworkAccessManager();

                // 发送get请求
                manager->get(request);

                passWord_user->setPassWord(ui->editNewPW->text());
                QMessageBox::information(this, "密码修改", "密码修改成功！", QMessageBox::Ok, QMessageBox::Ok);
                closePassWord();
            }
            else
            {
                QMessageBox::critical(this, "错误", "新密码不能和原密码相同！", QMessageBox::Ok, QMessageBox::Ok);
            }
        }
        else
        {
            QMessageBox::critical(this, "错误", "新密码不一致！", QMessageBox::Ok, QMessageBox::Ok);
        }
    }
    else
    {
        QMessageBox::critical(this, "错误", "密码错误！", QMessageBox::Ok, QMessageBox::Ok);
    }
}

void updatePassWord::closePassWord()
{
    this->close();
    ui->editPassWord->setText("");
    ui->editNewPW->setText("");
    ui->editNewAgain->setText("");
}

void updatePassWord::mousePressEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        address = event->globalPos();
    }
}
void updatePassWord::mouseMoveEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}
void updatePassWord::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}
